package com.example.myapplication.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.bean.HomesBanner;
import com.example.myapplication.bean.HomesListBean;
import com.example.myapplication.bean.SquaseBean;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class HomesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<HomesBanner.DataBean> bannerList = new ArrayList<>();
    public List<HomesListBean.DataBean.DatasBean> list = new ArrayList<>();
    private int ITEM_TYPE_ONE = 1;
    private int ITEM_TYPE_TWO = 2;
    private final LayoutInflater from;
    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void OnItemClick(int position,List<HomesListBean.DataBean.DatasBean> list);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setBannerList(List<HomesBanner.DataBean> bannerList) {
        this.bannerList.addAll(bannerList);
        notifyDataSetChanged();
    }

    public void setList(List<HomesListBean.DataBean.DatasBean> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public HomesAdapter(Context context) {
        this.context = context;
        from = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_ONE) {
            View view = from.inflate(R.layout.item_banner, parent, false);
            return new ViewHolderOne(view);
        } else {
            View view = from.inflate(R.layout.item_list, parent, false);
            return new ViewHolderTwo(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        int itemViewType = holder.getItemViewType();
        if (itemViewType == ITEM_TYPE_ONE) {
            ViewHolderOne viewHolderOne = (ViewHolderOne) holder;
            viewHolderOne.banner.setImages(bannerList)
                    .setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object path, ImageView imageView) {
                            Glide.with(context).load(bannerList.get(position).getImagePath()).into(imageView);
                        }
                    }).start();

        } else {
            ViewHolderTwo viewHolderTwo = (ViewHolderTwo) holder;
            HomesListBean.DataBean.DatasBean datasBean = list.get(position);
            viewHolderTwo.title.setText(datasBean.getTitle());
            viewHolderTwo.chapterName.setText(datasBean.getChapterName());
            viewHolderTwo.niceDate.setText(datasBean.getNiceDate());
            viewHolderTwo.superChapterName.setText(datasBean.getSuperChapterName());
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener!=null){
                    onItemClickListener.OnItemClick(position,list);
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_TYPE_ONE;
        } else {
            return ITEM_TYPE_TWO;
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolderOne extends RecyclerView.ViewHolder {
        public Banner banner;

        public ViewHolderOne(View rootView) {
            super(rootView);
            this.banner = (Banner) rootView.findViewById(R.id.banner);
        }

    }

    public static class ViewHolderTwo extends RecyclerView.ViewHolder {
        public TextView chapterName;
        public TextView niceDate;
        public TextView title;
        public TextView superChapterName;
        public ImageView iv;

        public ViewHolderTwo(View rootView) {
            super(rootView);
            this.chapterName = (TextView) rootView.findViewById(R.id.chapterName);
            this.niceDate = (TextView) rootView.findViewById(R.id.niceDate);
            this.title = (TextView) rootView.findViewById(R.id.title);
            this.superChapterName = (TextView) rootView.findViewById(R.id.superChapterName);
            this.iv = (ImageView) rootView.findViewById(R.id.iv);
        }

    }
}
